﻿// XServers.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

// xservers.cpp : 定义控制台应用程序的入口点。
//


#include "XCoreTcpServer.hpp"
#include <thread>
bool g_bRun = true;

#include "log4z/log4z.h"
using namespace zsummer;
void cmdThread(CXCoreTcpServer* pClient)
{
	while (true) {
		char cmdBuf[256] = { 0 };
		scanf("%s", cmdBuf);
		if (0 == strcmp(cmdBuf, "exit")) {
			g_bRun = false;
			LOGI("服务关闭..");
			LOGI("退出cmdThread线程.\n");
			break;
		}
		else if (0 == strcmp(cmdBuf, "msg")) {
			//DataHeader  *p = new DataHeader();
			//p->cmd = 0;
			//p->dataLength = sizeof(DataHeader);
			//pClient->SendDataToAll(p);
			//pClient->SendDataBufToAll("hello word ni hao a !!");
			//delete p;
			//p = NULL;
		}
		else {
			printf("不支持的命令.\n");
			LOGI("不支持的命令." << cmdBuf);
		}
	}
}
void Show(CXCoreTcpServer* pClient) {
	while (g_bRun)
	{
		pClient->ShowMsg();
		Sleep(5000);
	}

}
int main()
{
	zsummer::log4z::ILog4zManager::getPtr()->config("server.cfg");
	zsummer::log4z::ILog4zManager::getPtr()->start();

	LOGI("Server Start .." << 14567);

	CXCoreTcpServer ser;
	ser.InitSocket();
	ser.Bind(nullptr, 1204);
	ser.Listen(5);

	std::thread td(cmdThread, &ser);
	td.detach();

	std::thread ts(Show, &ser);
	ts.detach();


	while (g_bRun) {
		ser.OnRun();
	}
	ser.Close();
	return 0;
}

